package pers.cz.mybatis.listener;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import pers.cz.support.DbOperatorListener;
import pers.cz.support.DefaultDbOperatorListener;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;

/**
 * @program: PostGirl-panent
 * @description: 通过插件监听数据库操作
 * @author: Cheng Zhi
 * @create: 2023-02-24 12:24
 **/
@Intercepts(@Signature(
        type = StatementHandler.class,
        method = "prepare",
        args = { Connection.class, Integer.class }
))
public class OperationListener implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        BoundSql boundSql = statementHandler.getBoundSql();
        ParameterHandler parameterHandler = statementHandler.getParameterHandler();
        PreparedStatement parameterObject = (PreparedStatement) parameterHandler.getParameterObject();
        // 获取到原生sql，解析sql类型
        String sql = boundSql.getSql();
        DbOperatorListener dbOperatorListener = new DefaultDbOperatorListener();
        //dbOperatorListener

        return null;
    }

    @Override
    public Object plugin(Object target) {
        return null;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
